import { _decorator, Component, Label, Node, Sprite, UITransform, Vec3 } from 'cc';
import { GLOBALUtil } from './GlobalUtil';
const { ccclass, property } = _decorator;

type Data = {
    user_id: number,
    nickname: string,
    avatar_url: string,
    card_frame: JSON,
    designation: JSON,
    LastLoginTime: string,
}

@ccclass('OtherPlayerInfoManager')
export class OtherPlayerInfoManager extends Component {
    //因为InfoNode组件有拿出来独立使用的情况，因此脚本独立撰写
    @property({type: Node, tooltip: "头像"})
    private avatar: Node = null;
    @property({type: Node, tooltip: "昵称"})
    private Nickname: Node = null;
    @property({type: Node, tooltip: "id"})
    private UserID: Node = null;
    @property({ type: Node, tooltip: "称号"})
    private designation: Node = null;
    @property({ type: Node, tooltip: "登录时间"})
    private LastLoginTime: Node = null;

    private userInfo: Data;
    protected async onEnable() {
        try {
        // 获取数据库的信息，更新显示
        GLOBALUtil.loadNetImage(this.userInfo.avatar_url, (sp) => {
            this.avatar.getComponent(Sprite).spriteFrame = sp
        })

        this.Nickname.getComponent(Label).string = this.userInfo.nickname;
        this.UserID.getComponent(Label).string = `ID: ${this.userInfo.user_id}`;
        this.LastLoginTime.getComponent(Label).string = "最近登录："+this.userInfo.LastLoginTime;
        if (this.userInfo.designation != null) {
            if (this.userInfo.designation[1000] == 1) {
                this.designation.active = true;
            }
        }
        } catch (error) {
            console.log(error);
        }
    }

    public setUserInfo(data: Data) {
        this.userInfo = data;
    }
}


